package com.shanling.shanlingcontroller.utils;

import android.Manifest;
import android.app.Activity;


import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import rx.Observer;

/**
 * Created by ch1 on 2019/10/31.
 */

public class PermissionsUtils {
    private static onGetpermissons  onGet;
    public static void getPermisson(Activity activity,  String... permissions){
        RxPermissions rxPermissions = new RxPermissions(activity);
        rxPermissions.requestEach(permissions
        ).subscribe(new Consumer<Permission>() {
            @Override
            public void accept(Permission permission) throws Exception {
                if(permission.granted){
                    if (onGet != null) {
                        onGet.onGetPermissonSuccess();
                    }
                }else {
                    if (!permission.shouldShowRequestPermissionRationale) {
                        if (onGet != null) {
                            onGet.onGetPermissonFailed();
                        }
                    }
                }

            }
        });
    }

    public static onGetpermissons getPermissons() {
        return onGet;
    }
    public static void setPermissons(onGetpermissons permissons) {
        PermissionsUtils.onGet = permissons;
    }

    public interface  onGetpermissons{
        void onGetPermissonSuccess();
        void onGetPermissonFailed();
    }
}
